package com.bwie.home.backstage.controller;

import java.util.Arrays;
import java.util.Map;

import com.bwie.home.common.utils.OosSave;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.bwie.home.backstage.entity.HomeEntity;
import com.bwie.home.backstage.service.HomeService;
import com.bwie.home.common.utils.PageUtils;
import com.bwie.home.common.utils.R;
import org.springframework.web.multipart.MultipartFile;


/**
 * 
 *
 * @author sy
 * @email sunlightcs@gmail.com
 * @date 2021-09-16 15:12:37
 */
@RestController
@RequestMapping("backstage/home")
public class HomeController {
    @Autowired
    private HomeService homeService;

    @Autowired
    private OosSave oosSave;

    /**
     * 上传图片
     */
    @PostMapping("/upload")
    public R upload(@RequestParam MultipartFile file){
        String s = oosSave.saveObject(file.getOriginalFilename(), file);
        return R.ok().put("img",s);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params,@RequestBody HomeEntity homeEntity){
        PageUtils page = homeService.queryPage(params,homeEntity);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{homeId}")
    public R info(@PathVariable("homeId") Integer homeId){
		HomeEntity home = homeService.getById(homeId);

        return R.ok().put("home", home);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody HomeEntity home){
		homeService.save(home);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody HomeEntity home){
		homeService.updateById(home);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] homeIds){
		homeService.removeByIds(Arrays.asList(homeIds));

        return R.ok();
    }

}
